// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#include "OpenMeca/Item/PartUserPoint.hpp"
#include "OpenMeca/Item/PartUserPipe.hpp"
#include "OpenMeca/Core/System.hpp"
#include "OpenMeca/Core/UserItemCommonProperty.hpp"
#include "OpenMeca/Core/System.hpp"

#include "boost/serialization/export.hpp"
BOOST_CLASS_EXPORT(OpenMeca::Item::PartUserPoint)

namespace OpenMeca
{  
  namespace Core
  {

    template<>
    void
    ItemCommonProperty<OpenMeca::Item::PartUserPoint>::BuildIconSymbol()
    {
      QIcon icon;
      OpenMeca::Item::PartUserPoint::DrawIcon(iconSymbol_, Qt::gray);
    }

    template<>
    void 
    UserItemCommonProperty<OpenMeca::Item::PartUserPoint>::CreateAction_Specialized() 
    {
      CreateAction_NewWithSelection<OpenMeca::Item::PartUserPipe>();
    }

  }
}


namespace OpenMeca
{  
  namespace Item
  {


    
     
    const std::string 
    PartUserPoint::GetStrType()
    {
      return std::string("PartUserPoint");
    }


    void
    PartUserPoint::Init()
    {
      Core::Singleton< Core::UserItemCommonProperty< PartUserPoint > >::Get().CreateAction_Edit();
      Core::Singleton< Core::UserItemCommonProperty< PartUserPoint > >::Get().CreateAction_Delete();
      Core::Singleton< Core::UserItemCommonProperty< PartUserPoint > >::Get().CreateAction_Specialized();
    }


    void 
    PartUserPoint::DrawIcon(QIcon& icon, QColor color)
    {
      //3D ball effect
      const int size = Gui::MainWindow::Get().GetIconSize();
      const int margin = 6;
      QPixmap pixmap(QSize(size,size));
      pixmap.fill(QColor (0, 0, 0, 0));
      QPainter painter(&pixmap);

      const int middle = size/2;

      QRadialGradient gradient(middle, middle, middle, middle, middle);
      gradient.setColorAt(0, Qt::white);
      gradient.setColorAt(1, color);
      QBrush brush(gradient);

      painter.setBrush(brush);
      painter.setPen(Qt::black);

      painter.setBrush(brush);
      painter.setPen(Qt::black);
      painter.drawEllipse(margin, margin, size-2*margin, size-2*margin);
      icon = QIcon(pixmap);
    }


    
    PartUserPoint::PartUserPoint(Core::UserItem& parent)
      :PartUser(GetStrType(), parent),
       mustBeScaled_(false),
       scaleCoordinate_(boost::bind(&PartUser::GetReferenceFrame, boost::ref(*this)))
    {
    }

  
    
    
    PartUserPoint::~PartUserPoint()
    {
    }

    void 
    PartUserPoint::BeginDraw()
    {
      if (mustBeScaled_)
	{
	  const double scale = Core::System::Get().GetSettings().GetScalePart()/100.;
	  PartUser::GetPoint() = (scaleCoordinate_*scale).GetPoint();
	}
    }

    void 
    PartUserPoint::DrawShape()
    {
      const double scale = Core::System::Get().GetSettings().GetScalePart()/100.;
      Core::Drawable::DrawSphere(.02f*scale);
    }

    void 
    PartUserPoint::UpdateIcon()
    {
      PartUserPoint::DrawIcon(GetIcon(), GetColor().GetQColor());
    }
    
    void
    PartUserPoint::SetScaleCoordinate(double x, double y, double z)
    {
      mustBeScaled_ = true;
      scaleCoordinate_[0] = x;
      scaleCoordinate_[1] = y;
      scaleCoordinate_[2] = z;
    }

  }
}




